package com.sm.sales_management.controller;


import com.sm.sales_management.domain.Commodity;
import com.sm.sales_management.service.ICommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/commodity")
public class CommodityController {

    @Autowired
    ICommodityService commodityService;

    @RequestMapping("/allcommodity")
    public String allcommodity(Model model){
        List<Commodity> commodityList=commodityService.selectAllCommodity();
        model.addAttribute("commodityList",commodityList);
        return "commodity_list";

    }

    @RequestMapping("/delcommodity{id}")
    public String delcommodity(@PathVariable("id") int id){
        commodityService.delete(id);
        return "redirect:/commodity/allcommodity";
    }

    @RequestMapping("/toupdatecom")
    public String toupdatecom(){
        return "updatecom";
    }

    @RequestMapping("/updatecom")
    public String updatepur(Commodity commodity){
        commodityService.updateCommodity(commodity);
        return "redirect:/commodity/allcommodity";
    }

    @RequestMapping("/toaddcom")
    public String toaddcom(){

        return "addcommodity";
    }

    @RequestMapping("/addcommodity")
    public String addcom(Commodity commodity){
        commodityService.insertCommodity(commodity);
        return "redirect:/commodity/allcommodity";
    }
}
